package sitonya.entity.roles

import com.IceCreamQAQ.Yu.annotation.EventListener
import com.icecreamqaq.yudb.jpa.annotation.Transactional
import javax.inject.Inject

@EventListener
class RoleService {
    @Inject
    lateinit var iCocRole: ICocRole

    @Transactional
    fun cocRoleUpdate(cocRoleObj: CocRole) {
        val cocRole = iCocRole.findById(cocRoleObj.id, cocRoleObj.name)?.apply {
            this.roleProp = cocRoleObj.roleProp
            this.otherProp = cocRoleObj.otherProp
        } ?: cocRoleObj
        iCocRole.saveOrUpdate(cocRole)
    }

    @Transactional
    fun cocRoleUpdate(id: Long, name: String) {
        val cocRole = iCocRole.findById(id, name) ?: CocRole(id = id, name = name)
        iCocRole.saveOrUpdate(cocRole)
    }

    @Transactional
    fun cocRoleDelete(id: Long, name: String) {
        iCocRole.findById(id, name)?.apply {
            this.oid?.apply {
                iCocRole.delete(this)
            }
        }
    }
}